const express = require('express')

const app = express()

// 请求体解析中间件
const { urlencoded, json } = require('./middleware/request-body-resolution')
app.use(urlencoded)
app.use(json)

const nunjucks = require('nunjucks')
nunjucks.configure(`${__dirname}/views`, { autoescape: true })

// 页面
app.get('/form', (_, res) => {
  res.send(nunjucks.render('form.html'))
})

// application/x-www-form-urlencoded
app.post('/formData', (req, res) => {
  const { id } = req.body
  res.json({
    reqBodyType: 'application/x-www-form-urlencoded',
    id
  })
})

// application/json
app.post('/formJson', (req, res) => {
  const { id } = req.body
  res.json({
    reqBodyType: 'application/json',
    id
  })
})

app.listen(80, () => {
  console.log('express server running at http://127.0.0.1');
})